package com.itheima.crm.controller;

import com.itheima.crm.pojo.BaseDict;
import com.itheima.crm.pojo.Customer;
import com.itheima.crm.pojo.QueryVo;
import com.itheima.crm.service.BaseDictService;
import com.itheima.crm.service.CustomerService;
import com.itheima.crm.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private BaseDictService baseDictService;
    @Autowired
    private CustomerService customerService;

    //${fromType}大括号里面的值必须是配置文件中配置的名字
    @Value("${fromType}")
    //注入的变量可以是任意的名
    private String fromType1;
    @Value("${industryType}")
    private String industryType1;
    @Value("${levelType}")
    private String levelType1;

    //http://127.0.0.1/customer/list
    @RequestMapping("list")
    public String list(Model model, QueryVo queryVo) {

        try {
            //解决get请求的乱码问题,重新编码的方式
            String name = new String(queryVo.getCustName().getBytes("ISO-8859-1"), "UTF-8");
            queryVo.setCustName(name);
        } catch (Exception e) {
            e.printStackTrace();
        }


        //根据条件分页查询客户
        Page<Customer> page = customerService.search(queryVo);

        //把查询结果page放到Model中,传递给前台页面
        model.addAttribute("page", page);

        //把获取到的查询条件放到Model中,传递给页面,回显查询条件
        model.addAttribute("custName", queryVo.getCustName());
        model.addAttribute("custSource", queryVo.getCustSource());
        model.addAttribute("custIndustry", queryVo.getCustIndustry());
        model.addAttribute("custLevel", queryVo.getCustLevel());

        //客户来源
        List<BaseDict> fromType = baseDictService.queryByDictTypeCode(fromType1);

        //所属行业
        List<BaseDict> industryType = baseDictService.queryByDictTypeCode(industryType1);

        //客户级别
        List<BaseDict> levelType = baseDictService.queryByDictTypeCode(levelType1);


        //使用Model把数据传递给前台页面
        //客户来源
        model.addAttribute("fromType", fromType);
        //所属行业
        model.addAttribute("industryType", industryType);
        //客户级别
        model.addAttribute("levelType", levelType);


        return "customer";
    }

    //实现修改客户的回显客户数据
    //http://127.0.0.1/customer/edit.action?id=16
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    @ResponseBody
    public Customer queryById(Long id) {
        Customer customer = customerService.queryById(id);
        return customer;
    }

    //根据id修改客户
    //http://127.0.0.1/customer/update.action
    //查看页面是Ajax的调用,应该返回json格式的数据,
    //但是返回的数据并没有使用,所以返回什么都可以,不返回也可以
    //重点不是在返回数据是什么,重点是返回的是数据类型,而不是页面类型
    //@ResponseBody加入,就走转换器,返回的是数据类型
    //不加,走视图解析器,返回的是页面类型
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String updateById(Customer customer) {
        customerService.updateById(customer);
        return "修改成功啦~~~";
    }

    //根据id删除客户
    //http://127.0.0.1/customer/delete.action
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public void deleteById(Long id) {
        customerService.deleteById(id);
    }
}
